package web

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"strconv"
)

// User 用户管理
type User struct {
	UserBll *bll.User `inject:""`
}

// QueryPage 查询分页数据
func (a *User) QueryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	params := schema.UserQueryParam{
		Nickname:     ctx.Query("nickname"),
		RealName:     ctx.Query("real_name"),
		EnterpriseID: ctx.Query("enterprise_id"),
		Gender:       ctx.QueryInt("gender"),
		UserType:     ctx.Query("user_type"),
		AuthStatus:   ctx.QueryInt("auth_status"),
		Tel:          ctx.Query("tel"),
		Status:       ctx.QueryInt("status"),
		From:         ctx.QueryInt64("from"),
		To:           ctx.QueryInt64("to"),
	}

	if params.UserType == "0" {
		params.UserType = ""
	}

	total, items, err := a.UserBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// Query 查询多个用户
func (a *User) Query(ctx *context.Context) error {
	var (
		ids    = ctx.QueryList("id")
		item   *schema.User
		result = []*schema.User{}
		err    error
	)
	for _, v := range ids {
		item, err = a.UserBll.Get(v)
		if err != nil {
			return ctx.ResErrInternalServer(err)
		}
		if item != nil {
			result = append(result, item)
		}
	}
	return ctx.ResSuccess(result)
}

// Get 获取用户信息
func (a *User) Get(ctx *context.Context) error {
	item, err := a.UserBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(item)
}

// CancelAuth 用户取消认证
func (a *User) CancelAuth(ctx *context.Context) error {
	ctx.WriteOperateLog("Users.CancelAuth", "员工管理-取消认证")
	err := a.UserBll.CancelAuth(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// WriteOff 用户注销
func (a *User) WriteOff(ctx *context.Context) error {
	ctx.WriteOperateLog("Users.WriteOff", "员工管理-用户注销")
	err := a.UserBll.WriteOff(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// ChangeBan 修改用户禁止项
func (a *User) ChangeBan(ctx *context.Context) error {
	ctx.WriteOperateLog("User.ChangeBan", "员工管理-修改禁止项")

	var (
		userID string
		ty     string
		act    string
		value  bool
		err    error
	)

	userID = ctx.Param("id")
	ty = ctx.Param("type")
	act = ctx.Param("act")

	switch ty {
	case "ban_msg":
	case "ban_login":
	case "ban_essay":
	case "ban_comment":
	default:
		ctx.ResErrBadRequest(fmt.Errorf("非法参数"))
	}

	value, err = strconv.ParseBool(act)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}
	info := map[string]interface{}{
		ty: value,
	}
	err = a.UserBll.UserModel.Update(userID, info)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Count 用户计数
func (a *User) Count(ctx *context.Context) error {

	result, err := a.UserBll.Count()
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(result)
}

// Search 用户查询
func (a *User) Search(ctx *context.Context) error {
	var (
		search = ctx.Query("search")
		count  = ctx.QueryInt("count", 10)
		result []*schema.UserShow
		err    error
	)

	result, err = a.UserBll.Search(search, count)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(result)
}
